package ee.subjects.frontend.controller.command.employeeCommands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ee.subjects.backend.model.data.Employee;
import ee.subjects.backend.model.data.Employee_role;
import ee.subjects.backend.model.data.Employee_role_type;
import ee.subjects.backend.model.data.Enterprise;
import ee.subjects.backend.model.data.Person;
import ee.subjects.backend.services.EmployeeService;
import ee.subjects.backend.services.EmployeeServiceImpl;
import ee.subjects.backend.services.EnterpriseService;
import ee.subjects.backend.services.EnterpriseServiceImpl;
import ee.subjects.backend.services.PersonService;
import ee.subjects.backend.services.PersonServiceImpl;
import ee.subjects.frontend.controller.command.Command;
import ee.subjects.frontend.controller.model.CustomEmployeeRole;

public class EmployeeRoles implements Command {

	@Override
	public int execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		if(request.getParameter("saveRoles")!=null){
			this.saveRoles(request);
			request.setAttribute ("Saved", "1");
		}else{
			request.setAttribute ("Saved", "0");
		}
		
		if(request.getParameter("enterpriseId")!=null && request.getParameter("personId")!=null){
			int personId = Integer.parseInt(request.getParameter("personId"));
			int enterpriseId = Integer.parseInt(request.getParameter("enterpriseId"));
			int employeeId=0;
			
			EmployeeService employeeService = new EmployeeServiceImpl();
			
			// Get employee id
			List<Employee> allEmployeeObjectsPersonHas = employeeService.getAllEmployeeObjectsPersonHas(personId);
			for(Employee employee : allEmployeeObjectsPersonHas){
				if(employee.getPerson_fk()==personId && employee.getEnterprise_fk()==enterpriseId){
					employeeId=employee.getEmployee();
					break;
				}
			}
			
			List<Employee_role> allRolesThatEmployeeHas = employeeService.getAllRolesThatEmployeeHas(employeeId);
			List<Employee_role_type> allEmployeeRoleTypes = employeeService.getAllEmployee_role_types();
			List<CustomEmployeeRole> customEmployeeRoleList = new ArrayList<CustomEmployeeRole>();
			for(Employee_role_type roleType :allEmployeeRoleTypes){
				CustomEmployeeRole customEmployeeRole = new CustomEmployeeRole();
				customEmployeeRole.setRoleTypeId(roleType.getEmployee_role_type());
				customEmployeeRole.setRoleTypeName(roleType.getType_name());
				
				for(Employee_role role :allRolesThatEmployeeHas){
					if(role.getEmployee_role_type_fk()==roleType.getEmployee_role_type()){
						customEmployeeRole.setSelected(true);
					}
				}
				customEmployeeRoleList.add(customEmployeeRole);
			}
			
			request.setAttribute ("EmployeeRoleList", customEmployeeRoleList);
			
			PersonService personService = new PersonServiceImpl();
			Person person = personService.getPersonById(personId);
			request.setAttribute ("Person", person);
			
			EnterpriseService enterpriseService = new EnterpriseServiceImpl();
			Enterprise enterprise = enterpriseService.getEnterpriseById(enterpriseId);
			request.setAttribute ("Enterprise", enterprise);
			
			request.setAttribute ("EmployeeId", Integer.toString(employeeId));
			
		}
		return 0;
	}

	private void saveRoles(HttpServletRequest request) {
		int employeeId = Integer.parseInt(request.getParameter("employeeId"));
		EmployeeService employeeService = new EmployeeServiceImpl();
		
		// First removea all roles
		List<Employee_role> allRolesThatEmployeeHas = employeeService.getAllRolesThatEmployeeHas(employeeId);
		for(Employee_role role :allRolesThatEmployeeHas){
			employeeService.removeRoleFromEmployee(employeeId, role.getEmployee_role());
		}
	
		String[] outerArray=request.getParameterValues("roleSelection");
		for (int i = 0; i < outerArray.length; i++) {
			employeeService.addRoleToEmployee(employeeId, Integer.parseInt(outerArray[i]), "Y");      
	    }
	}

}
